package cn.stylefeng.guns.modular.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.Inventory;
import cn.stylefeng.guns.modular.entity.User;
import cn.stylefeng.guns.modular.mapper.InventoryMapper;
import cn.stylefeng.guns.modular.mapper.UserCompanyMapper;
import cn.stylefeng.guns.modular.util.IdWorker;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author XiangTianGe
 * @since 2020-04-07
 */
@Service
public class UserCompanyServiceImpl extends ServiceImpl<UserCompanyMapper, User> {


    @Autowired
    private UserCompanyMapper userCompanyMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private InventoryMapper inventoryMapper;

    /**
     * 获取用户列表
     */
    public LayuiPageInfo getList(Page page, String beginTime, String endTime, String keyword) {
        // 组装查询条件
        QueryWrapper<User> qw = new QueryWrapper<>();
        if (!Strings.isNullOrEmpty(beginTime) && !Strings.isNullOrEmpty(endTime)) {
            qw.between("DATE(created_at)", beginTime, endTime);
        }
        if (!Strings.isNullOrEmpty(keyword)) {
            qw.like("user_name", keyword)
                    .or().like("company_name", keyword)
                    .or().like("logon_id", keyword);
        }
        qw.eq("user_role", "1");
        qw.orderByDesc("created_at");
        IPage iPage = userCompanyMapper.selectMapsPage(page, qw);
        List<Map<String, Object>> records = iPage.getRecords();
        for (int i = 0; i < records.size(); i++) {
            Map<String, Object> map = records.get(i);
            if (!map.get("user_role").toString().equals("1")) {
                records.remove(i);
            }
        }
        return LayuiPageFactory.createPageInfo(iPage);
    }


    /**
     * 下级列表
     */
    public LayuiPageInfo juniorList(Page page, Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_user_id", userId);
        queryWrapper.orderByDesc("created_at");
        IPage iPage = userCompanyMapper.selectMapsPage(page, queryWrapper);
        return LayuiPageFactory.createPageInfo(iPage);
    }

    /**
     * 冻结
     */
    public void freeze(Long userId) {
        this.baseMapper.freeze(userId);
    }

    /**
     * 解冻
     */
    public void unfreeze(Long userId) {
        this.baseMapper.unfreeze(userId);
    }

    /**
     * 新增用户
     */
    public ResponseData addCompany(User user) {
        String logonId = user.getLogonId();
        User logonUser = userCompanyMapper.selectLogonId(logonId);
        if (logonUser != null) {
            return ResponseData.error("用户已注册!");
        }
        user.setUserRole(1);
        user.setCreatedAt(new Date());
        user.setUpdatedAt(user.getCreatedAt());
        user.setUserId(idWorker.nextId());
        // 创建用户
        this.baseMapper.insert(user);
        // 创建库存
        Inventory inventory = new Inventory();
        inventory.setUserId(user.getUserId());
        inventoryMapper.insert(inventory);
        return ResponseData.success();
    }
}
